import 'package:encrypt/encrypt.dart' as encrypt;

String keyStr =
    "K9lgsUNdK53p6zyuf5Z5jY0kNnAGtZfx"; // 测试： K9lgsUNdK53p6zyuf5Z5jY0kNnAGtZfx 正式：SfCSGQs7BlQnjQ3VtD3ZKSJfWKo5KUcN

String ivStr = "1234567890123456";

class AESUtil {
  static String encryptAes(plainText) {
    final key = encrypt.Key.fromUtf8(keyStr);
    final iv = encrypt.IV.fromUtf8(ivStr);
    final encrypter =
        encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.ecb));
    final encrypted = encrypter.encrypt(plainText, iv: iv);

    return encrypted.base64;
  }

  static String decryptAes(encryptedStr) {
    final key = encrypt.Key.fromUtf8(keyStr);
    final iv = encrypt.IV.fromUtf8(ivStr);
    final encrypter =
        encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.ecb));
    final decrypted =
        encrypter.decrypt64(base64UrlDecode(encryptedStr), iv: iv);

    return decrypted;
  }
}

String base64UrlDecode(String input) {
  // 恢复不安全的字符
  String paddedInput = input.replaceAll('-', '+').replaceAll('_', '/');
  // 补齐等号
  int padding = paddedInput.length % 4;
  if (padding > 0) {
    paddedInput += '=' * (4 - padding);
  }
  return paddedInput;
}

void main(List<String> args) {
  AESUtil.decryptAes(base64UrlDecode(
      'K3A6EwNMWh6BBFMCtVf6FS0LDAx6ewhJUsOl5L-IjbKzoaFwbolWxwRP9VBCflQkaO7hAdBe9Xm18tSk8THtuiXSwxpuybR0bFG4Ba0oJvWo1hdv-56zl2Dr8loQ-MydEXeHRLBcO-F2--nGLx0i9LEx2hAhMFV8GmI5EihEjPZmBrYcsjGRjDsgOMyDbIrkzt10UD9eNRsjRI1u6ziWNLTvW1KNXeL3DMqvJ08XGiiY7fU2_T3PaKr0u9QEsCLPphP189rTbW_VKcBi_yCtNfER5wL1Lc0SmmxY3xwLDx6nDsV6BynfDkRmxsuHX5fw7bYwWBrETOgR2pGwxu9641fBdegWnoeeIa5qhTK4n7VxZ72XIP8kzGHT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